package com.rockcent.mall.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.domain.Customer;
import com.rockcent.mall.lib.domain.CustomerBank;
import com.rockcent.mall.lib.dto.CustomerBankDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.BankRepository;
import com.rockcent.mall.lib.repository.CustomerBankRepository;
import com.rockcent.mall.lib.repository.CustomerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhangjh on 2016/9/23.
 */
@RestController
public class CustomerBankController {

    private final Logger log = LoggerFactory.getLogger(CustomerBankController.class);

    @Autowired
    private BaseUserHelper userHelper;

    @Autowired
    private CustomerBankRepository customerBankRepository;


    @Autowired
    private CustomerBankRepository customerBankCardDao;

    @Autowired
    private CustomerRepository customerDao;

    @Autowired
    private BankRepository bankDao;

    @Autowired
    private ObjectMapper objectMapper;



    /**
     * @api {get} /customer/bank/info 用户银行卡信息读取
     * @apiGroup customer_bank
     * @apiVersion 1.0.0
     *
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     * @apiParam {Number} bankCardId 用户银行卡ID
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     *  "data": {
     *      "id": 1,
     *      "bankTypeName": "中国农业银行",
     *      "accountNo": "1111111111",
     *      "bankCardHolder": "444444",
     *      "customerPhone": "13926988206"
     *  }
     * }
     */
    @RequestMapping(value = Routes.CUSTOMER_BANK_INFO, method = RequestMethod.GET)
    public JsonResult info(@RequestParam("bankCardId") Long bankCardId, HttpServletRequest request) {
        UserInfoDto userInfoDto=userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (bankCardId != null) {

                CustomerBankDto customerBank = customerBankRepository.showCustomerBankInfor(bankCardId);
                jsonResult.data = customerBank;
            }
        };
        return doing.go(request, objectMapper, log);
    }


    /**
     *
     * @api {get} /customer/bank/update  用户银行卡信息更新
     * @apiGroup customer_bank
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     * @apiParam  id:10
     * @apiParam {String} customerPhone: 客户联系电话
     * @apiParam {String} bankCardHolder: 持有人
     * @apiParam {String} accountNo: 银行账号
     * @apiParam {Long} bankTypeId: 银行类型
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     * }
     */
    @Transactional
    @RequestMapping(value = Routes.CUSTOMER_BANK_UPDATE, method = RequestMethod.GET)
    public JsonResult update(CustomerBankDto bankCardInfor, HttpServletRequest request) {
        UserInfoDto userInfoDto=userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (bankCardInfor != null) {
                CustomerBank card = customerBankCardDao.findOne(bankCardInfor.getId());
                Long customerId = card.getCustomerId();
                Customer customer = customerDao.findOne(customerId);
                customer.setMobile(bankCardInfor.getCustomerPhone());

                if (card != null) {
                    card.setCardHolder(bankCardInfor.getBankCardHolder());
                    card.setBankCard(bankCardInfor.getAccountNo());
                    if (bankDao.exists(bankCardInfor.getBankTypeId())) {
                        card.setBankTypeId(bankCardInfor.getBankTypeId());
                    }
                }
                customerBankCardDao.save(card);
                customerDao.save(customer);
            }
        };

        return doing.go(request, objectMapper, log);
    }
}
